package com.face.server.service.impl;

import com.face.server.entity.*;
import com.face.server.repository.AdminRepository;
import com.face.server.repository.DormitoryBuildRepository;
import com.face.server.repository.EventLogRepository;
import com.face.server.repository.JoinedRepository;
import com.face.server.service.WarningService;
import com.util.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

/**
 * @author cong
 * @date 2018/3/27
 */
@Service
public class WarningServiceImpl implements WarningService {

    @Autowired
    AdminRepository adminRepository;

    @Autowired
    EventLogRepository eventLogRepository;

    @Autowired
    JoinedRepository joinedRepository;

    @Autowired
    DormitoryBuildRepository dormitoryBuildRepository;

    public static Date getCalender(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH,-5);
        return c.getTime();
    }


    @Override
    public List<Warning> getWarns() {
        List<Integer> buildList;
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin)subject.getPrincipal();
        buildList = admin.getDormitoryNumList();
        if(admin.isSuper()){
            List<DormitoryBuild> dormitoryBuilds = dormitoryBuildRepository.findAll();
            dormitoryBuilds.stream().forEach(e->buildList.add(e.getBuildingNum()));
        }

        Set<Warning> warns = new HashSet<>();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH,-5);
        Date date = c.getTime();
        List<EventLog> eventLogs = eventLogRepository.findByBuildNumberInAndTypeIsAndTimeAfter(buildList,0,date);

        HashSet<String> eventName = new HashSet<>();
        eventLogs.stream().forEach(e->
            eventName.add(e.getName())
        );
        List<Joined> joineds = joinedRepository.findByDormitoryBuildNumInAndTypeIs(buildList,0);
        Set<String> joinedName = new HashSet<>();
        joineds.stream().forEach(e->{
            joinedName.add(e.getName());
        });
        Warning warning;
        Iterator<String> iterableName = joinedName.iterator();
        while(iterableName.hasNext()){
            String name = iterableName.next();
            if(!(eventName.contains(name))){
                List<EventLog> eventLogs1 = eventLogRepository.findByNameAndTypeIsAndBuildNumberIn(name,0,buildList);
                EventLog eventLog = new EventLog();
                double timeInterval=0;
                double nowTimeInterval;
                for(EventLog eventLog1:eventLogs1){
                    nowTimeInterval = new Date().getTime()-eventLog1.getTime().getTime();
                    if(timeInterval==0){
                        timeInterval = nowTimeInterval;
                        eventLog = eventLog1;
                        continue;
                    }
                    if(nowTimeInterval<timeInterval){
                        timeInterval = nowTimeInterval;
                        eventLog = eventLog1;
                    }
                }
                int dormitoryBuild = eventLog.getBuildNumber();
                Joined joined = joinedRepository.findByNameAndDormitoryBuildNumIsAndTypeIs(name,dormitoryBuild,0);
                if(joined==null) continue;
                warning = new Warning();
                warning.setTime(eventLog.getTime());
                warning.setName(name);
                warning.setDormAddress(joined.getDormitoryBuildNum()+"幢"+joined.getDormitoryNum()+"室");
                warning.setPhone(joined.getPhone());
                warning.setImg(eventLog.getImg());
                warning.setFromToday(DateUtils.daysBetween(eventLog.getTime(),new Date()));
                warns.add(warning);
            }
        }
        List<Warning> warningList = new ArrayList<>();
        warningList.addAll(warns);
        return warningList;
    }

    @Override
    public List<Warning> getPartWarns(String name,String begin,String end) throws ParseException {
        List<Warning> warningList = getWarns();
        Date beginDate=new Date();
        Date endDate = new Date();
        if(begin!=null&&end!=null){
            beginDate = DateUtils.parse(begin);
            endDate = DateUtils.parse(end);
        }else{
            warningList.stream().forEach(e->{
                if(e.getName()!=name){
                    warningList.remove(e);
                }
            });
            return warningList;
        }
        if(name ==null){
            Date finalBeginDate = beginDate;
            Date finalEndDate = endDate;
            warningList.stream().forEach((Warning e) ->{
                if((finalBeginDate.getTime() >= e.getTime().getTime()) || finalEndDate.getTime() <=e.getTime().getTime()){
                    warningList.remove(e);
                }
            });
            return warningList;
        }else{
            for(Warning warning:warningList){
                if(warning.getName()!=name||warning.getTime().getTime()<=beginDate.getTime()||
                        warning.getTime().getTime()>=endDate.getTime()){
                    warningList.remove(warning);
                }
            }
            return warningList;
        }

    }

    @Override
    public List<Warning> getWarnsByTime(String name, String time) throws ParseException {
        Date date = new Date();
        date = DateUtils.parse(time);
        List<Warning> warningList = getWarns();
        if(name==null){
            Date finalDate = date;
            warningList.stream().forEach(e->{
                if(e.getTime()!= finalDate){
                    warningList.remove(e);
                }
            });
            return warningList;
        }else {
            for(Warning warning:warningList){
                if(warning.getName()!=name||warning.getTime()!=date){
                    warningList.remove(warning);
                }
            }
            return warningList;
        }

    }

}
